//
//  CarpoolingDetailViewController.m
//  cityStar
//
//  Created by joyban on 15-1-20.
//  Copyright (c) 2015年 tiny. All rights reserved.
//

#import "CarpoolingDetailViewController.h"

#import "CarpoolingDetailCell.h"
#import "RIButtonItem.h"
#import "UIAlertView+Blocks.h"

NSString *const CarpoolingDetailCellID = @"carpoolingDetailCellID";

@interface CarpoolingDetailViewController ()<UIAlertViewDelegate>

@property (weak, nonatomic) IBOutlet UILabel *titlleLabel;
@property (weak, nonatomic) IBOutlet UILabel *typeLabel;
@property (weak, nonatomic) IBOutlet UILabel *addressLabel;
@property (weak, nonatomic) IBOutlet UILabel *tujinLabel;
@property (weak, nonatomic) IBOutlet UILabel *timeLabel;
@property (weak, nonatomic) IBOutlet UILabel *nubLabel;

@property (weak, nonatomic) IBOutlet UIButton *Consumptionbtn;
@property (weak, nonatomic) IBOutlet UITableView *CarpoolingDeatailTable;
@property (strong, nonatomic) IBOutlet UIView *heardView;
@property (weak, nonatomic) IBOutlet UILabel *carTypelabel;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *ringtline;
@property (weak, nonatomic) IBOutlet UILabel *addressLable;

@end

@implementation CarpoolingDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"拼车详情";
    [self addBackButton];
    [self setUpTableViews];
    self.CarpoolingDeatailTable.tableHeaderView  = self.heardView;
    [self setdatdacarpool];
    if([self.withchType isEqualToString:@"cityService"])
    {
        [self.Consumptionbtn setTitle:@"消费积分查看联系方式" forState:UIControlStateNormal];
    }else{
        [self.Consumptionbtn setTitle:@"信息作废" forState:UIControlStateNormal];
        [self.Consumptionbtn setTitle:@"信息作废" forState:UIControlStateHighlighted];
        
        [self.Consumptionbtn setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
    }
}

-(void)setdatdacarpool
{
    
    NSString *typestrinf = [NSString stringWithFormat:@"%@",self.carModel.carpool_type];
    if([typestrinf isEqualToString:@"0"])
    {
        self.typeLabel.text = @"上下班拼车";
    }else if([typestrinf isEqualToString:@"1"])
    {
        self.ringtline.constant = 225;
        self.typeLabel.text = @"单程旅行拼车";
    }else{
        self.ringtline.constant = 195;
        self.typeLabel.text = @"往返双程旅行拼车";
    }
    self.titlleLabel.text = self.carModel.title;
    NSInteger typestring = [[NSString stringWithFormat:@"%@",self.carModel.car_model] integerValue];
    switch (typestring) {
        case 0:
            self.carTypelabel.text = @"经济型";
            break;
        case 1:
            self.carTypelabel.text = @"舒适型";
            break;
        case 2:
            self.carTypelabel.text = @"豪华型";
            break;
        default:
            break;
    }
    self.addressLabel.text = [NSString stringWithFormat:@"%@-%@",self.carModel.start,self.carModel.end];
    NSString *str = @"";
    NSArray *discountArr = [[NSString stringWithFormat:@"%@",self.carModel.way] componentsSeparatedByString:@" "];
    if(discountArr.count > 1)
    {
        NSString *string = discountArr[0];
        for (int i = 1; i<discountArr.count; i++) {
            string = [NSString stringWithFormat:@"%@ %@",string,discountArr[i]];
        }
        str = string;
    }else{
        str = discountArr[0];
    }
    self.addressLable.text = [NSString stringWithFormat:@"%@",self.carModel.district_name];
    self.tujinLabel.text =[NSString stringWithFormat:@"%@",str];
    self.nubLabel.text = [NSString stringWithFormat:@"%@人",self.carModel.people];
    NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:[[NSString stringWithFormat:@"%@",self.carModel.departure] intValue]];
    NSDateFormatter* formater = [[NSDateFormatter alloc] init];
    [formater setDateFormat:@"YYYY-MM-dd HH:mm"];
    NSString *  locationString = [formater stringFromDate:confromTimesp];
    self.timeLabel.text = locationString;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)setUpTableViews
{
    [self.CarpoolingDeatailTable registerNib:[CarpoolingDetailCell nib] forCellReuseIdentifier:CarpoolingDetailCellID];
}
#pragma mark - Actions
-(IBAction)getContact:(id)sender
{
    if ([self.withchType isEqualToString:@"cityService"]) {
        NSString *postUrl = [NSString stringWithFormat:@"%@%@/%i",KinterFace_PublishContact,self.carModel._id,[self.carModel.type intValue]];
        [HttpRequestManager getMethod:postUrl parameters:nil completedMainBlock:^(BOOL success, id response, NSString *errMsg) {
            if (success) {
                DLog(@"%@",response);
                DLog(@"%@",response);
                if(![self.Consumptionbtn.titleLabel.text isEqualToString:@"消费积分查看联系方式"])
                {
                    NSString *string = [NSString stringWithFormat:@"%@",response[@"value"][@"contacts"][@"phone"]];
                    NSString *telStr=[@"tel://" stringByAppendingString:string];
                    NSURL *url=[NSURL URLWithString:telStr];
                    [[UIApplication sharedApplication]openURL:url];
                }else{
                    UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"是否消耗积分查看联系方式" message:nil delegate:self cancelButtonTitle:@"是" otherButtonTitles:@"否",nil];
                    [alertView show];
                }
            }else{
                [SVProgressHUD showErrorWithStatus:errMsg];
            }
        }];
    }else{
        RIButtonItem *itemCancel = [RIButtonItem itemWithLabel:@"取消"];
        RIButtonItem *itemSubMit = [RIButtonItem itemWithLabel:@"确定" action:^{
            NSString *postUrl = [NSString stringWithFormat:@"%@%@",KinterFace_PublishInvalid,self.carModel._id];
            [HttpRequestManager deleteMethod:postUrl parameters:nil completedMainBlock:^(BOOL success, id response, NSString *errMsg) {
                if (success) {
                    [SVProgressHUD showSuccessWithStatus:@"信息作废成功"];
                }else{
                    [SVProgressHUD showErrorWithStatus:@"信息作废失败"];
                }
            }];
        }];
        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"是否作废该信息" message:nil cancelButtonItem:itemCancel otherButtonItems:itemSubMit, nil];
        [alertView show];
    }
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;
{
    if (buttonIndex == 0) {
        NSString *postUrl = [NSString stringWithFormat:@"%@%@/%i",KinterFace_PublishContact,self.carModel._id,[self.carModel.type intValue]];
        [HttpRequestManager getMethod:postUrl parameters:nil completedMainBlock:^(BOOL success, id response, NSString *errMsg) {
            if (success) {
                DLog(@"%@",response);
                NSString *string = [NSString stringWithFormat:@"%@",response[@"value"][@"contacts"][@"phone"]];
                
                NSString *title = [NSString stringWithFormat:@"%@:%@",self.carModel.contact,string];
                [self.Consumptionbtn setTitle:title forState:UIControlStateNormal];
            }else{
                [SVProgressHUD showErrorWithStatus:errMsg];
            }
        }];
    }
}
#pragma mark - UITableViewDelegate
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *ss = [NSString stringWithFormat:@"%@",self.carModel.desc];
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 220, 25)];
    label.font = [UIFont systemFontOfSize:14];
    [label setLineBreakMode:NSLineBreakByWordWrapping];
    [label setNumberOfLines:0];
    NSString *_Detail = [NSString stringWithFormat:@"%@",ss];
    label.text = _Detail;
    [label sizeToFit];
    float labelheight = label.frame.size.height;
    return [CarpoolingDetailCell heightOfTableCell] - (90 - labelheight);
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

#pragma mark - UITableViewDataSource
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CarpoolingDetailCell *carpoolingDetailCell = [tableView dequeueReusableCellWithIdentifier:CarpoolingDetailCellID];
    carpoolingDetailCell.selectionStyle = UITableViewCellSelectionStyleNone;
    carpoolingDetailCell.contentLabel.text = self.carModel.desc;
    return carpoolingDetailCell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
